﻿// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
//
// DirectXPage.xaml.h
// Declaration of the DirectXPage class.
//

#pragma once

#include "DirectXPage.g.h"

#include "DeviceResources.h"
#include "Logger.h"
#include "UserSettings.h"
#include "WeathrMain.h"

namespace Weathr
{
    /// <summary>
    /// A page that hosts a DirectX SwapChainBackgroundPanel.
    /// This page must be the root of the Window content (it cannot be hosted on a Frame).
    /// </summary>
    [Windows::UI::Xaml::Data::Bindable]
    [Windows::Foundation::Metadata::WebHostHidden]
    public ref class DirectXPage sealed
    {
    public:
        DirectXPage(Windows::UI::Xaml::Controls::Frame^ rootFrame);
        virtual ~DirectXPage();

        void SaveInternalState(Windows::Foundation::Collections::IPropertySet^ state);
        void LoadInternalState(Windows::Foundation::Collections::IPropertySet^ state);

        property Windows::Foundation::Collections::IObservableVector<LogMessage^>^ LogMessages
        {
            Windows::Foundation::Collections::IObservableVector<LogMessage^>^ get()
            {
                return m_logMessages;
            }
        }

        property Windows::Foundation::Collections::IObservableVector<LogMessage^>^ ActionMessages
        {
            Windows::Foundation::Collections::IObservableVector<LogMessage^>^ get()
            {
                return m_actionMessages;
            }
        }

    private:
        Windows::Foundation::EventRegistrationToken m_renderToken;

        // XAML low-level rendering event handler.
        void OnRendering(Platform::Object^ sender, Platform::Object^ args);

        // Window event handlers.
        void OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args);

        // Display information event handlers.
        void OnDpiChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args);
        void OnOrientationChanged(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args);
        void OnDisplayContentsInvalidated(Windows::Graphics::Display::DisplayInformation^ sender, Platform::Object^ args);

        // Other event handlers.
        void OnCompositionScaleChanged(Windows::UI::Xaml::Controls::SwapChainPanel^ sender, Object^ args);
        void OnSwapChainPanelSizeChanged(Platform::Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e);

        // Handles incoming log messages.
        void OnLogMessageAdded(Platform::Object^ sender, LogMessage^ message);

        // Track our independent input on a background worker thread.
        //Windows::Foundation::IAsyncAction^ m_inputLoopWorker;
        //Windows::UI::Core::CoreIndependentInputSource^ m_coreInput;

        // Independent input handling functions.
        //void OnPointerPressed(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
        //void OnPointerMoved(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
        //void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);

        // Resources used to render the DirectX content in the XAML page background.
        std::shared_ptr<DeviceResources> m_deviceResources;
        std::unique_ptr<WeathrMain> m_main;
        bool m_windowVisible;

        // Event data and handler for logging.
        Platform::Collections::Vector<LogMessage^>^ m_logMessages;      // logs completed actions (both synchronous and asynchronous.)
        Platform::Collections::Vector<LogMessage^>^ m_actionMessages;   // logs actions that are active in the background.

       // void OnKeyDown(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::KeyEventArgs ^args);
       // void OnKeyUp(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::KeyEventArgs ^args);
    };
}

